//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "HestonModel.h"
using namespace Cephei::QL::Models::Equity;
#include <gen/QL/Processes/HestonProcess.h>
#include <gen/QL/Models/CalibrationHelper.h>
#include <gen/QL/Math/Optimization/OptimizationMethod.h>
#include <gen/QL/Math/Optimization/EndCriteria.h>
#include <gen/QL/Math/Optimization/Constraint.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/Models/CalibratedModel.h>
using namespace Cephei::QL::Processes;
using namespace Cephei::QL::Models;
using namespace Cephei::QL::Math::Optimization;
using namespace Cephei::QL::Math;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Equity::CHestonModel::CHestonModel (Cephei::QL::Processes::IHestonProcess^ process) : CCalibratedModel(CHestonModel::typeid)
{
    CHestonProcess^ _Cprocess;
    try
    {
#ifdef HANDLE
        _phHestonModel = NULL;
#endif
        _Cprocess = safe_cast<CHestonProcess^> (process);
        _Cprocess->Lock();
        boost::shared_ptr<QuantLib::HestonProcess>& _process = static_cast<boost::shared_ptr<QuantLib::HestonProcess>&> (_Cprocess->GetShared ()); 
        _ppHestonModel = new boost::shared_ptr<QuantLib::HestonModel> (new QuantLib::HestonModel ( _process ));
        SetCalibratedModel (boost::dynamic_pointer_cast<QuantLib::CalibratedModel> (*_ppHestonModel));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
    }
}
Cephei::QL::Models::Equity::CHestonModel::CHestonModel (boost::shared_ptr<QuantLib::HestonModel>& childNative, Object^ owner) : CCalibratedModel(CHestonModel::typeid)
{
#ifdef HANDLE
	_phHestonModel = NULL;
#endif
	_ppHestonModel = &childNative;
    _ppCalibratedModel = new boost::shared_ptr<QuantLib::CalibratedModel> (boost::dynamic_pointer_cast<QuantLib::CalibratedModel> (*_ppHestonModel));
}
Cephei::QL::Models::Equity::CHestonModel::CHestonModel (QuantLib::HestonModel& childNative, Object^ owner) : CCalibratedModel(CHestonModel::typeid)
{
#ifdef HANDLE
	_phHestonModel = NULL;
#endif
	_ppHestonModel = new boost::shared_ptr<QuantLib::HestonModel> (&childNative);
    _ppCalibratedModel = new boost::shared_ptr<QuantLib::CalibratedModel> (boost::dynamic_pointer_cast<QuantLib::CalibratedModel> (*_ppHestonModel));
    _HestonModelOwner = owner;
    _CalibratedModelOwner = owner;
}

Cephei::QL::Models::Equity::CHestonModel::CHestonModel (CHestonModel^ copy) : CCalibratedModel(CHestonModel::typeid)
{
#ifdef HANDLE
	_phHestonModel = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppHestonModel = new boost::shared_ptr<QuantLib::HestonModel> (copy->GetShared());
        _ppCalibratedModel = new boost::shared_ptr<QuantLib::CalibratedModel> (boost::dynamic_pointer_cast<QuantLib::CalibratedModel> (*_ppHestonModel));
    }
}
Cephei::QL::Models::Equity::CHestonModel::CHestonModel (System::Type^ t) : CCalibratedModel(CHestonModel::typeid)
{
#ifdef HANDLE
	_phHestonModel = NULL;
#endif
	if (!t->IsSubclassOf(CHestonModel::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Equity::CHestonModel::CHestonModel (QuantLib::Handle<QuantLib::HestonModel>& childNative, Object^ owner)  : CCalibratedModel(CHestonModel::typeid)
{
	_phHestonModel = &childNative;
	_ppHestonModel = &static_cast<boost::shared_ptr<QuantLib::HestonModel>>(childNative.currentLink());
    _ppCalibratedModel = new boost::shared_ptr<QuantLib::CalibratedModel> (boost::dynamic_pointer_cast<QuantLib::CalibratedModel> (*_ppHestonModel));
    _HestonModelOwner = owner;
}
Cephei::QL::Models::Equity::CHestonModel::CHestonModel (QuantLib::Handle<QuantLib::HestonModel> childNative)  : CCalibratedModel(CHestonModel::typeid)
{
	_phHestonModel = &childNative;
	_ppHestonModel = &static_cast<boost::shared_ptr<QuantLib::HestonModel>>(childNative.currentLink());
    _ppCalibratedModel = new boost::shared_ptr<QuantLib::CalibratedModel> (boost::dynamic_pointer_cast<QuantLib::CalibratedModel> (*_ppHestonModel));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Equity::CHestonModel::CHestonModel (QuantLib::HestonModel childNative)  : CCalibratedModel(CHestonModel::typeid)
{
#ifdef HANDLE
	_phHestonModel = NULL;
#endif
	_ppHestonModel = new boost::shared_ptr<QuantLib::HestonModel> (new QuantLib::HestonModel (childNative));
    _ppCalibratedModel = new boost::shared_ptr<QuantLib::CalibratedModel> (boost::dynamic_pointer_cast<QuantLib::CalibratedModel> (*_ppHestonModel));
}
#endif

Cephei::QL::Models::Equity::CHestonModel::~CHestonModel ()
{
    if (_ppHestonModel != NULL)
    {
	    delete _ppHestonModel;
        _ppHestonModel = NULL;
    }
}
Cephei::QL::Models::Equity::CHestonModel::!CHestonModel ()
{
    if (_ppHestonModel != NULL)
    {
	    delete _ppHestonModel;
    }
}
QuantLib::HestonModel& Cephei::QL::Models::Equity::CHestonModel::GetReference ()
{
    if (_ppHestonModel == NULL) throw gcnew NativeNullException ();
	return **_ppHestonModel;
}
boost::shared_ptr<QuantLib::HestonModel>& Cephei::QL::Models::Equity::CHestonModel::GetShared ()
{
    if (_ppHestonModel == NULL) throw gcnew NativeNullException ();
	return *_ppHestonModel;
}
QuantLib::HestonModel* Cephei::QL::Models::Equity::CHestonModel::GetPointer ()
{
    if (_ppHestonModel == NULL) throw gcnew NativeNullException ();
	return &**_ppHestonModel;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::HestonModel>& Cephei::QL::Models::Equity::CHestonModel::GetHandle ()
{
	if (_phHestonModel == NULL)
	{
		_phHestonModel = new Handle<QuantLib::HestonModel> (*_ppHestonModel);
	}
	return *_phHestonModel;
}
#endif
bool Cephei::QL::Models::Equity::CHestonModel::HasNative () 
{
	return (_ppHestonModel != NULL);
}

Double Cephei::QL::Models::Equity::CHestonModel::Kappa::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHestonModel)->kappa ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Processes::IHestonProcess^ Cephei::QL::Models::Equity::CHestonModel::Process::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::HestonProcess> _rv = (boost::shared_ptr<QuantLib::HestonProcess>)(*_ppHestonModel)->process ( );   
        Cephei::QL::Processes::CHestonProcess^ _nrv = gcnew Cephei::QL::Processes::CHestonProcess (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Equity::CHestonModel::Rho::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHestonModel)->rho ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Equity::CHestonModel::Sigma::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHestonModel)->sigma ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Equity::CHestonModel::Theta::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHestonModel)->theta ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Equity::CHestonModel::V0::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHestonModel)->v0 ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Equity::IHestonModel^ Cephei::QL::Models::Equity::CHestonModel_Factory::Create (Cephei::QL::Processes::IHestonProcess^ process)
{
    return gcnew CHestonModel ( process);
}
